package com.example;

import com.example.config.ThreadPoolConfig;
import com.example.task.TimePrintTask;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class Application {
    public static void main(String[] args) {
        // 初始化Spring上下文，加载配置类
        AnnotationConfigApplicationContext context =
                new AnnotationConfigApplicationContext(ThreadPoolConfig.class);


        ThreadPoolExecutor executor = context.getBean("taskExecutor", ThreadPoolExecutor.class);
        TimePrintTask task = context.getBean(TimePrintTask.class);
        executor.submit(task);

        // 等待任务完成后关闭资源
        try {
            executor.shutdown();
            if (!executor.awaitTermination(70, TimeUnit.SECONDS)) {
                executor.shutdownNow();
            }
        } catch (InterruptedException e) {
            executor.shutdownNow();
        } finally {
            context.close();
        }
    }
}
